#include <cstdlib>
#include <iostream>

#include "myLog.h"

myLog::myLog(std::string strName)
{
    init();
    setCallerName(strName);
}

myLog::~myLog()
{
    fclose(fp);
}

void myLog::init()
{
    std::string strFileName;
    strFileName = "log" + getDateTime(1) + ".txt";
    //printf("%s\n",strFileName.c_str());
    fp = fopen(strFileName.c_str(),"a");
}

void myLog::setCallerName(std::string strName)
{
    strCallerName = strName;
}

std::string myLog::getDateTime(int outType)
{
    std::string rtnValue;
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    char buffer [20];
    switch (outType)
    {
    case 1:
        strftime(buffer,80,"%Y%m%d",timeinfo);
        break;
    case 2:
        strftime(buffer,80,"%Y:%m:%d %H:%M:%S",timeinfo);
        break;
    }
    rtnValue = buffer;
    return rtnValue;
}


void myLog::writelog(std::string msg)
{
    fprintf(fp,"%s [%s] %s\n",getDateTime(2).c_str(),strCallerName.c_str(), msg.c_str());
    fflush (fp);
}

void myLog::displaylog(std::string msg)
{
	std::cout << msg << std::endl;
}
